package com.fxu.framework.biz.base;

import cn.hutool.core.util.NumberUtil;
import lombok.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * SQL语句设计中的limits
 * @author fangxu 
 * @version 1.0.0
 */
@Getter
@ToString
public class Limits {
	private final static String SPLIT_CHAR = ",";
	public final static String SPLIT_CHILD = ":";
	private final static int SPLIT_CHILD_LENGTH = SPLIT_CHAR.length();

	@Getter
	@NoArgsConstructor
	@AllArgsConstructor
	public static class Dict {
		private Integer intKey;
		private String strKey;
		private String value;
	}

	private String limits; // 原来的limits
	private String[] array; // 切割后到数组
	private boolean multiple; //是否多选
	private List<Dict> dictList = new ArrayList<>();
	
	public Limits(@NotNull String limits) {
		this(limits, false);
	}
	
	public Limits(@NotNull String limits, boolean multiple) {
		super();
		this.limits = limits;
		this.multiple = multiple;
		if (limits != null && limits.length() > 0) {
			this.array = limits.split(SPLIT_CHAR);
			for (String tmp : this.array) {
				int index = tmp.indexOf(SPLIT_CHILD);
				if (index > -1) {
					String test = tmp.substring(0, index);
					if (NumberUtil.isInteger(test)) {
						dictList.add(new Dict(Integer.parseInt(test), null, tmp.substring(index + SPLIT_CHILD_LENGTH)));
					} else {
						dictList.add(new Dict(null, test, tmp.substring(index + SPLIT_CHILD_LENGTH)));
					}
				} else {
					dictList.add(new Dict(null, tmp, tmp));
				}
			}
		}
	}
}
